package com.cartoon.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cartoon.R;
import com.cartoon.adapter.CarttonHistoryAdapter;
import com.cartoon.databinding.CartoonSearchrecordActivityBinding;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

import anime.room.HistoryModel;

import base.QActivity;
import cartoon.room.CartoonHistoryModel;
import cartoon.room.CartoonHistoryViewModel;
import constant.AppConstants;

public  class CartoonSearchRecordActivity  extends QActivity {

    private CartoonSearchrecordActivityBinding binding;
    private CartoonHistoryViewModel viewModel;
    private CarttonHistoryAdapter carttonHistoryAdapter;
    private LinkedHashMap<String, String> hashMap = new LinkedHashMap();
    private ArrayList<String> newList = new ArrayList<>();
    @Override
    public int getLayoutRes() {
        return R.layout.cartoon_searchrecord_activity;
    }

    @Override
    public void initView() {

        binding = DataBindingUtil.setContentView(this, getLayoutRes());
    }

    @Override
    public void initData() {

        viewModel = new ViewModelProvider(this).get(CartoonHistoryViewModel.class);



        FlexboxLayoutManager manager = new FlexboxLayoutManager(this, FlexDirection.ROW, FlexWrap.WRAP) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        binding.rvHistory.setLayoutManager(manager);
        carttonHistoryAdapter = new CarttonHistoryAdapter(R.layout.carrton_history_item, null);
        binding.rvHistory.setAdapter(carttonHistoryAdapter);


    }

    @Override
    public void initListener() {


        carttonHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String item = (String) adapter.getData().get(position);
                binding.edSearch.setText(item);
                getLRYSearch();

            }
        });

        viewModel.getAllSLive().observe(this, new Observer<List<CartoonHistoryModel>>() {
            @Override
            public void onChanged(List<CartoonHistoryModel> historyModels) {
                newList.clear();

                for (int i = 0; i < historyModels.size(); i++) {
                    hashMap.put(historyModels.get(i).getName(), historyModels.get(i).getName());
                }

                for (LinkedHashMap.Entry<String, String> entry : hashMap.entrySet()) {

                    newList.add(entry.getValue());
                }
                Collections.reverse(newList);

                carttonHistoryAdapter.replaceData(newList);
            }
        });


        binding.edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            getLRYSearch();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }

        });

        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getLRYSearch();
            }
        });


        binding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.deleteTableData();

                SPStaticUtils.put("CartoonsearchId", 0);

                v.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        newList.clear();
                        carttonHistoryAdapter.replaceData(newList);
                        ToastUtils.showLong("已清空搜索记录");
                    }
                }, 100);


            }
        });
    }

    public void getLRYSearch() {
        String searchName = binding.edSearch.getText().toString().trim();

        if (TextUtils.isEmpty(searchName)) {
            ToastUtils.showLong("请输入搜索内容");
            return;
        }
        int searchId = SPStaticUtils.getInt("CartoonsearchId", 0);
        int newsearchId = searchId + 1;
        LogUtils.d("保存的Id" + newsearchId);

        SPStaticUtils.put("CartoonsearchId", newsearchId);

        viewModel.addHistoryDao(new CartoonHistoryModel(newsearchId, searchName));

        viewModel.getAllSLive().getValue();

        Intent intent = new Intent(this, CarttonSearchRecordActivity.class);
        intent.putExtra("searchName", searchName);
        startActivity(intent);
    }



    @Override
    public void networkMessage() {

    }
}
